package controller

import (
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"net/http"
	"vpn/app/global"
	"vpn/app/global/consts"
	"vpn/app/http/service"
	"vpn/app/utils/file_handle"
	"vpn/app/utils/response"
)

type ChangePassWordForm struct {
	OldPwd     string `json:"oldPwd"`
	CurrentPwd string `json:"currentPwd"`
}

func GetUserInfo(context *gin.Context) {
	var userInfo = service.UserInfo{
		Name:     consts.InitUserName,
		PassWord: consts.InitUserPwd,
	}
	exist, err := file_handle.Exists(global.Conf.UserInfoFilePath)
	if err != nil {
		log.Error(err.Error())
		response.Fail(context, http.StatusInternalServerError, "获取用户信息失败", gin.H{"error": err.Error()})
		return
	}
	if !exist {
		response.Success(context, "", gin.H{"userInfo": userInfo, "changed": false})
		return
	}
	err = file_handle.ReadJsonFile(global.Conf.UserInfoFilePath, &userInfo)
	if err != nil {
		log.Error(err.Error())
		response.Fail(context, http.StatusInternalServerError, "获取用户信息失败", gin.H{"error": err.Error()})
		return
	}
	response.Success(context, "", gin.H{"userInfo": userInfo, "changed": true})
}

func SaveUserInfo(context *gin.Context) {
	var userInfo service.UserInfo
	if err := context.ShouldBindJSON(&userInfo); err != nil {
		log.Error(err.Error())
		response.Fail(context, http.StatusInternalServerError, "保存异常", gin.H{"error": err.Error()})
		return
	}
	err := file_handle.SaveJsonFile(userInfo, global.Conf.UserInfoFilePath)
	if err != nil {
		log.Error(err.Error())
		response.Fail(context, http.StatusInternalServerError, "保存失败", gin.H{"error": err.Error()})
		return
	}
	response.Success(context, "保存成功", gin.H{})
}

func Login(context *gin.Context) {
	var (
		userInfoForm service.UserInfo
		userInfo     service.UserInfo
	)
	if err := context.ShouldBindJSON(&userInfoForm); err != nil {
		log.Error(err.Error())
		response.Fail(context, http.StatusInternalServerError, "登录异常", gin.H{"error": err.Error()})
		return
	}
	exist, _ := file_handle.Exists(global.Conf.UserInfoFilePath)
	if !exist {
		if userInfoForm.Name == consts.InitUserName && userInfoForm.PassWord == consts.InitUserPwd {
			service.SetJwtToken(context, userInfoForm.Name)
			response.Success(context, "登录成功!", gin.H{})
			return
		} else {
			response.Fail(context, http.StatusBadRequest, "账户或密码错误!", gin.H{})
			return
		}
	}
	if err := file_handle.ReadJsonFile(global.Conf.UserInfoFilePath, &userInfo); err != nil {
		log.Error(err.Error())
		response.Fail(context, http.StatusInternalServerError, "登录异常", gin.H{"error": err.Error()})
		return
	}
	if userInfo.PassWord != userInfoForm.PassWord || userInfoForm.Name != userInfo.Name {
		response.Fail(context, http.StatusBadRequest, "用户名或密码错误", gin.H{})
		return
	}
	service.SetJwtToken(context, userInfo.Name)
	response.Success(context, "登录成功", gin.H{})
}

func ChangePassWord(context *gin.Context) {
	var userInfoForm ChangePassWordForm
	if err := context.ShouldBindJSON(&userInfoForm); err != nil {
		log.Error(err.Error())
		response.Fail(context, http.StatusInternalServerError, "登录异常", gin.H{"error": err.Error()})
		return
	}

	if userInfoForm.OldPwd != service.GetUserPassword(global.Conf.UserInfoFilePath) {
		response.Fail(context, http.StatusBadRequest, "旧密码输入错误", gin.H{})
		return
	}
	if userInfoForm.CurrentPwd == userInfoForm.OldPwd {
		response.Fail(context, http.StatusBadRequest, "当前密码不能和旧密码相同", gin.H{})
		return
	}
	UserInfo := service.UserInfo{
		Name:     consts.InitUserName,
		PassWord: userInfoForm.CurrentPwd,
	}
	if err := file_handle.SaveJsonFile(UserInfo, global.Conf.UserInfoFilePath); err != nil {
		response.Fail(context, http.StatusInternalServerError, "服务异常", gin.H{"error": err.Error()})
		return
	}
	response.Success(context, "修改成功", gin.H{})
}
